﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Axiom.CodeManifold.WPF.Converters
{
	public static class RedGreenTrafficLight
	{
		static RedGreenTrafficLight()
		{
			BitmapImage bmp = new BitmapImage();
			bmp.BeginInit();
			bmp.UriSource = new Uri("pack://application:,,,/Axiom.CodeManifold.WPF;component/Assets/Images/Icons/blue.png");
			bmp.EndInit();
			Green = bmp;

			bmp = new BitmapImage();
			bmp.BeginInit();
			bmp.UriSource = new Uri("pack://application:,,,/Axiom.CodeManifold.WPF;component/Assets/Images/Icons/red.png");
			bmp.EndInit();
			Red = bmp;
		}
		public static ImageSource Green { get; set; }
		public static ImageSource Red { get; set; }

	}

	[ValueConversion(typeof(bool), typeof(ImageSource))]
	public class BoolToRedOrGreenImage : IValueConverter
	{
		static BoolToRedOrGreenImage()
		{
			BitmapImage bmp = new BitmapImage();
			bmp.BeginInit();
			bmp.UriSource = new Uri("pack://application:,,,/Axiom.CodeManifold.WPF;component/Assets/Images/Icons/blue.png");
			bmp.EndInit();
			Image1 = bmp;

			bmp = new BitmapImage();
			bmp.BeginInit();
			bmp.UriSource = new Uri("pack://application:,,,/Axiom.CodeManifold.WPF;component/Assets/Images/Icons/red.png");
			bmp.EndInit();
			Image2 = bmp;
		}

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			ImageSource result = null;
			if ((bool)value)
			{

				result = Image1;
			}
			else
			{
				result = Image2;
			}
			return result;
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return DependencyProperty.UnsetValue;
		}

		private static ImageSource Image1 { get; set; }
		private static ImageSource Image2 { get; set; }
	}

	[ValueConversion(typeof(object), typeof(ImageSource))]
	public class ObjectRefToRedOrGreenImage : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			object result = null;
			if (value == null)
			{
				result = RedGreenTrafficLight.Red;
			}
			else
			{
				result = RedGreenTrafficLight.Green;
			}
			return result;
		}
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return DependencyProperty.UnsetValue;
		}
	}

	[ValueConversion(typeof(string), typeof(string))]
	public class SplitPascalCamelCaseConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			string result = null;

			if (string.IsNullOrWhiteSpace((string)value))
			{
				return string.Empty;
			}
			Regex regex = new Regex(@"(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])", RegexOptions.Compiled);
			result = regex.Replace((string)value," $1").Trim();

			return result;
		}
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return DependencyProperty.UnsetValue;
		}
	}
}


